class Subject{
    constructor(){
        this.subs=[]
        this.state=0
    }
    addSub(wacher){
        this.subs.push(wacher)
    }
    getState(){
        return this.state
    }
    setState(state){
        this.state = state
        this.motify()
    }
    motify(){
        this.subs.forEach(w=>{
            w.update()
        })
    }
}
class Observer{
    constructor(name, subject){
        this.name = name
        this.subject = subject
        this.subject.addSub(this)
    }
    update(){
        console.log('变了',this.name,this.subject.getState())
    }
}
let subject = new Subject()
let observe = new Observer('观察她',subject)
let observe1 = new Observer('观察她1',subject)
subject.setState(100)